import  pymysql
class MySQLHelper:
    def __init__(self):
        try:
            self.conn=pymysql.connect(host="localhost",user="root",password="root",port=3306,database="blogdb")
            print("conn success")
        except Exception as e:
            print("conn error:",e)

    def executeForUCD(self,sql,params):
        self.cuser=self.conn.cursor();
        i=self.cuser.execute(sql,params)
        self.conn.commit();
        self.close()
        print(i)
        return i


    def executeForSelectOne(self,sql,params):
        self.cursor = self.conn.cursor()
        self.cursor.execute(sql, params)
        rs=self.cursor.fetchone()
        print(rs)
        self.close()
        return rs

    def executeForSelectAll(self,sql,parms):
        self.cursor=self.conn.cursor()
        self.cursor.execute(sql,parms)
        rs=self.cursor.fetchall()
        # print(rs)
        return rs

    def close(self):
        try:
            self.conn.close()
            self.cuser.close()
        except Exception as e:
            print("error=",e)
        finally:
            print("finally")


# pymysql=MySQLHelper()
# sql="insert into tb_user values(null,%s,%s,%s)"
# pymysql.executeForUCD(sql,["lizhao","11111",1])